﻿using ShopB2C.Core.Entities;

namespace ShopB2C.Core.Helper
{
    public static class JavaScriptHelper
    {
        /// <summary>
        /// 只创建弹出式警告消息脚本
        /// </summary>
        /// <param name="message">消息正文</param>
        /// <returns>alert(message)</returns>
        public static string Alert(string message)
        {
            return string.IsNullOrEmpty(message) ? string.Empty : string.Format("alert(\"{0}\");" , message);
        }
        /// <summary>
        /// 弹出警告消息，然后跳转到指定页面
        /// </summary>
        /// <param name="alertType">弹出式消息类型</param>
        /// <param name="message">消息正文</param>
        /// <param name="operate">针对消息类型要进行的操作</param>
        /// <returns>脚本字符串'</returns>
        public static string Alert(string message , AlertType alertType , string operate)
        {
            string script = string.Empty;
            switch (alertType)
            {
                case AlertType.ClosePage:
                    script = "window.close();";
                    break;
                case AlertType.GoBack:
                    script = string.Format("history.go({0})" , operate);
                    break;
                case AlertType.RedirectToURL:
                    script = string.Format("location.href=\"{0}\";" , operate);
                    break;
                case AlertType.SpecifiedProcess:
                    script = operate;
                    break;
            }
            return Alert(message) + script;
        }
    }
}
